package logic

import (
	"context"
	"errors"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"mygozero/user/internal/svc"
	"mygozero/user/internal/types"
	"mygozero/user/model"
)

type UserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {
	return &UserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLogic) User(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {

	user, err := l.svcCtx.UserModel.FindOne(l.ctx, 1)
	if err != nil && err != model.ErrNotFound {
		//return nil, errors.New("查询数据失败")
		return nil, err
	}
	if user == nil {
		return nil, errors.New("查询数据失败")
	}
	resp = &types.UserInfoResp{}
	err = copier.Copy(resp, user)

	// 使用api调用rpc
	//in := usercenter.GetUserInfoReq{Id: req.Id}
	//infoResp, err := l.svcCtx.UserRpcClient.GetUserInfo(l.ctx, &in)
	//if err != nil {
	//	return nil, err
	//}
	//resp = new(types.UserInfoResp)
	//err = copier.Copy(&resp.UserInfo, infoResp.User)
	//if err != nil {
	//	return nil, err
	//}
	return resp, err
}
